package com.tiancan.design.user.service.impl;

import com.tiancan.design.user.bean.Result;
import com.tiancan.design.user.bean.pojo.Token;
import com.tiancan.design.user.dao.TokenDao;
import com.tiancan.design.user.service.CreateTokenService;
import com.tiancan.design.user.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TokenServiceMySqlImpl implements TokenService {
  @Autowired private CreateTokenService createTokenService;
  @Autowired private TokenDao tokenDao;

  @Override
  public void storageToken(String email, String token) {
    Token token1 = new Token();
    token1.setEmail(email);
    token1.setToken(token);
    tokenDao.save(token1);
  }

  @Override
  public String getToken(String key) {
    return createTokenService.getToken(key);
  }

  @Override
  public Result<String> info(String token) {
    Result<String> result = new Result<>();
    List<Token> listByToken = tokenDao.findListByToken(token);
    if (listByToken == null || listByToken.size() != 1) {
      result.setResult(false);
      result.setRes("token无效");
      return result;
    }
    Token token1 = listByToken.get(0);
    result.setResult(true);
    result.setData(token1.getEmail());
    return result;
  }
}
